<template>
	<div>
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
					<el-row>
						<el-col :span="6">
							<el-form-item label="分配时间">
								<el-date-picker v-model="tableData.dateRange" type="datetimerange" size="default"
									style="width: 300px" range-separator="-" start-placeholder="开始日期"
									end-placeholder="结束日期"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :span="6" class="colBlock">
							<el-form-item label="分配记录ID" prop="id">
								<el-input v-model="tableData.param.id" placeholder="请输入分配记录ID" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="6" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item label="分配企业" prop="companyCode">
								<el-select v-model="tableData.param.companyCode" placeholder="请选择分配企业" clearable>
									<el-option v-for="dict in companySelectlist" :key="`company-${dict.value}`" :label="dict.label"
										:value="dict.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6" class="colBlock">
							<el-form-item label="企业名称" prop="companyName">
								<el-input v-model="tableData.param.companyName" placeholder="请输入企业名称" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="6" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item label="状态" prop="baseStatus">
								<el-select v-model="tableData.param.baseStatus" placeholder="请选择状态" clearable>
									<el-option v-for="dict in dictionary.statusList" :key="dict.value" :label="dict.label"
										:value="dict.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item label="客户经理" prop="manageCode">
								<el-select v-model="tableData.param.manageCode" placeholder="请选择客户经理" clearable>
									<el-option v-for="dict in managerSelectlist" :key="`manage-${dict.value}`" :label="dict.label"
										:value="dict.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6" :class="!showAll ? 'colBlock' : 'colNone'">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" link @click="toggleSearch">
									{{ word }}
									<el-icon v-show="showAll">
										<ArrowUp />
									</el-icon>
									<el-icon v-show="!showAll">
										<ArrowDown />
									</el-icon>
								</el-button>
							</el-form-item>
						</el-col>
						<el-col :span="6" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" link @click="toggleSearch">
									{{ word }}
									<el-icon v-show="showAll"><ArrowUp /></el-icon>
									<el-icon v-show="!showAll"><ArrowDown /></el-icon>
								</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<el-table v-loading="loading" :data="tableData.data">
				<el-table-column label="分配记录ID" align="center" prop="id" min-width="100px" />
				<el-table-column label="手机号" align="center" prop="phone" min-width="100px" />
				<el-table-column label="分配企业名称" align="center" prop="companyName" min-width="100px" />
				<el-table-column label="使用状态" align="center" prop="statusTxt" min-width="100px" />
				<el-table-column label="分配时间" align="center" prop="createTimeTxt" min-width="100px" />
				<el-table-column label="客户经理" align="center" prop="customerName" min-width="100px" />
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="getList" />
		</el-card>
	</div>
</template>
<script lang="ts">
import { allocationRecordsListApi,companySelectApi,managerSelectApi } from '@/api/numberManage/allocationRecords';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import {  FormInstance } from 'element-plus';
import { formatDateTime, updateParamsWithDateRange } from '@/utils/index'
export default defineComponent({
	name: 'allocationRecordsList',	//卡分配记录
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		// 是否显示所有搜索选项
		const showAll = ref(false);

		const word = computed(() => {
			if (showAll.value === false) {
				//对文字进行处理
				return '展开搜索';
			} else {
				return '收起搜索';
			}
		});
		const state = reactive({
			dictionary : {
			"ENABLED":"启用",
			"SUSPENDED":"欠费停机",
			"DISABLED":"禁用",
			statusList:[
				{value:'ENABLED',label:'启用'},
				{value:'SUSPENDED',label:'欠费停机'},
				{value:'DISABLED',label:'禁用'},
			]
		},
		companySelectlist:[],	//企业下拉
		managerSelectlist:[],	//客户经理下拉
			tableData: {
				data: [],
				total: 0,
				loading: false,
				dateRange: [],
				param: {
					pageNumber: 1,
					pageLength: 10,
					companyCode: undefined,
					companyName: undefined,
					baseStatus: undefined,
					manageCode: undefined,
					endTime: undefined,
					beginTime: undefined,
					id: undefined
				},
			},
		});
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
			getcompanySelect();
			managerSelect()
		};
		/** 重置按钮操作 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			state.tableData.dateRange = []
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			const obj = updateParamsWithDateRange(state.tableData.dateRange)
			Object.assign(state.tableData.param, obj);
			allocationRecordsListApi(state.tableData.param).then((res: any) => {
				let list = res.data.list ?? [];
				state.tableData.data = list.map(item => {
					item.createTimeTxt = formatDateTime(item.createTime)
					item.statusTxt = state.dictionary[item.status]
					return item
				})
				console.log(state.tableData.data);
				state.tableData.total = res.data.total;
				loading.value = false;
			});
		};
		// 获取企业下拉
		const getcompanySelect=()=>{
			companySelectApi().then((res:any)=>{
				if(res?.data){
					let list=res.data
					for (let key in list){
						let obj={
							value:key,
							label:list[key]
						}
						state.companySelectlist.push(obj)
					}
				}
			})
		}
		// 获取企业经理下拉
		const managerSelect=()=>{
			managerSelectApi().then((res:any)=>{
				if(res?.data){
					let list=res.data
					for (let key in list){
						let obj={
							value:key,
							label:list[key]
						}
						state.managerSelectlist.push(obj)
					}
				}
			})
		}
		const toggleSearch = () => {
			showAll.value = !showAll.value;
		};
		return {
			proxy,
			showAll,
			loading,
			word,
			queryRef,
			resetQuery,
			getList,
			toggleSearch,
			...toRefs(state),
		};
	},
});
</script>
<style lang="scss" scoped>
.colBlock {
	display: block;
}

.colNone {
	display: none;
}
</style>
